<?php
namespace app\command;

use app\beautiful\model\setting\SettingSmsModel;
use app\common\logic\SmsLogic;
use app\common\model\MemberConsumeModel;
use app\common\model\MemberMoneyDetailModel;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Memberconsume_com extends Command
{
    protected function configure()
    {
        $this->setName('Memberconsume_com')
            ->addArgument('name', Argument::OPTIONAL, "your name")
            ->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
            ->setDescription('Say Hello');
    }

    protected function execute(Input $input, Output $output)
    {
        $h=date('H');
        $i=date('i');
        $name = trim($input->getArgument('name'));
        if(($h!='00' || $i!='10') && empty($name)){
            $output->writeln("暂不能执行！每天凌晨01分执行" );exit();
        }
        $page=1;
        $num=100;
        $order['a.id']='asc';
        do{
            $s=[];
            $s['status']=1;
            $s['end_time']=date('Y-m-d H:i:s');
            $list=MemberConsumeModel::ulist($s,$order,$page,$num);
            if(empty($list['list'])){
                $output->writeln("暂无数据".var_export([$s,$order,$page],true) );exit();
            }
            foreach ($list['list'] as $v){
                $up['status']=2;
                $up['reward_money']=0;
                if($v['partake_status']==1){//入围---奖励计算
                    $m_s=[];
                    $m_s[]=['status','IN',[1,2]];
                    $m_s[]=['partake_status','=',1];
                    $m_s[]=['type','=',$v['type']];
                    $m_s[]=['start_time','=',$v['start_time']];
                    $m_s[]=['shop_id','=',$v['shop_id']];
                    $totaL_money=MemberConsumeModel::where($m_s)->sum('total_money');
                    $reward_money=round($v['all_reward_money']*($v['total_money']/$totaL_money),2);
                    $up['reward_money']=$reward_money;
                    if($reward_money>0){
                        MemberMoneyDetailModel::in($reward_money,1,3,$v['shop_id'],$v['member_id'],0,$v['id']);
                        //发送短信通知
                        $sms_setting = SettingSmsModel::where([['shop_id','=',$v['shop_id']]])->find();
                        if(null !== $sms_setting && !empty($v['mobile'])){
                            if($sms_setting->consume_notice==1){
                                $sms_logic= new SmsLogic();
                                $params=[
                                    'user'=>(string)$v['nick_name'],
                                    'name'=>(string)$v['shop_name'],
                                ];
                                $sms_logic->send($v['shop_id'],[$v['mobile']],$params,'MemberConsume');
                            }
                        }
                    }
                }
                MemberConsumeModel::up($up,['id'=>$v['id']]);
            }
//            $page++;
        }while(true);
    }
}